﻿/// <reference path="jquery-ui-1.8.custom.min.js" />

//2010-10-10 新版 tipsWindown ..
// By X-Soft copyright.
// 
//属性
var showWindown = true;
var templateSrc = "loading.gif"; //设置loading.gif路径 
var autoCloseTimer;

//对象
//用于传递参数的全局单例变量
this.BoxTag = new Object();
this.tipsSend = this;
//按钮事件回调
var callBackArg = { handled: true, Sender: new Object, Text: "" };


//main
tipsWindow = function (options) {
    //参数初始化
    options = options || {};
    //设置默认参数
    getDefaultOptions(options);
    // main Box Div Load...
    $("#windown-box").remove(); //请除内容
    var width = width >= 950 ? this.width = 950 : this.width = options.width;     //设置最大窗口宽度
    var height = height >= 527 ? this.height = 527 : this.height = options.height;  //设置最大窗口高度
    //var resizable = resizable == null || resizable == 'undefined' ? "true" : resizable;
    if (showWindown == true) {
        var simpleWindown_html = new String;
        simpleWindown_html = "<div id=\"windownbg\" style=\"height:" + $(document).height() + "px;filter:alpha(opacity=0);opacity:0;z-index: 999901\"></div>";
        simpleWindown_html += "<div id=\"windown-box\">";
        //        simpleWindown_html += "<div id=\"windown-title\"><h2></h2><span id=\"windown-close\">关闭</span></div>";
        simpleWindown_html += "<div id=\"windown-title\"><h2></h2><button title=\"关闭\" id=\"windown-close\"><span class=\"none\">╳</span></button></div>";
        simpleWindown_html += "<div id=\"windown-content-border\"><div id=\"windown-content\"></div></div>";
        if (options.isConfirm) {
            var btnOkText = options.okBtnText ? options.okBtnText : "确 定";
            var btnCancelText = options.cancelBtnText ? options.cancelBtnText : "取 消";
            simpleWindown_html += "<div id=\"windown-box-controller\"><input  id=\"windown-btn-ok\" type=\"button\" value=\"" + btnOkText + "\"/>&nbsp;<input id=\"windown-btn-cancel\" type=\"button\" value=\"" + btnCancelText + "\"/>";
        }
        else if (options.showBottomPanel) {
            var btnOkText = typeof options.okBtnText != 'undefined' ? options.okBtnText : "确 定";
            simpleWindown_html += "<div id=\"windown-box-controller\"><input id=\"windown-btn-ok\" type=\"button\" value=\"" + btnOkText + "\"/>";

        }
        simpleWindown_html += "</div>";
        $("body").append(simpleWindown_html);
    }
    $("#windown-close").hover(function () {
        $(this).css({ "background": "url(\"/themes/images/default/tips/pupop_bg.png\") no-repeat scroll -37px -30px transparent" });
    }, function () {
        $(this).css({ 'background': "url(\"/themes/images/default/tips/pupop_bg.png\") no-repeat scroll 0px -30px transparent" });
    });
    //判断类型,内容
    contentType = options.type ? options.type : "text";
    content = options.content ? options.content : "";
    var title = options.title ? options.title : "";
    switch (contentType) {
        case "text":
            $("#windown-content").html(content);
            break;
        case "id":
            var obj = $("#" + content);
            if (obj.css("display") == "none")
            { obj.css("display", "block"); }
            $("#windown-content").html($("#" + content + "").html());
            break;
        case "img":
            $("#windown-content").ajaxStart(function () {
                $(this).html("<img src='" + templateSrc + "' class='loading' />");
            });
            $.ajax({
                error: function () {
                    $("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
                },
                success: function (html) {
                    $("#windown-content").html("<img src=" + content + " alt='' />");
                }
            });
            break;
        case "url":
            var content_array = content.split("?");
            $("#windown-content").ajaxStart(function () {
                $(this).html("<img src='" + templateSrc + "' class='loading' />");
            });
            $.ajax({
                type: content_array[0],
                url: content_array[1],
                data: content_array[2],
                error: function () {
                    $("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
                },
                success: function (html) {
                    $("#windown-content").html(html);
                }
            });
            break;
        case "iframe":
            $("#windown-content").ajaxStart(function () {
                $("#windown-title h2").html("loading..." + "<img src='" + templateSrc + "' class='loading' />");
                $("#windown-content").html("数据加载中...<img src='" + templateSrc + "' class='loading' />");
            });
            $.ajax({
                error: function () {
                    $("#windown-content").html("<p class='windown-error'>加载数据出错...</p>");
                },
                success: function (html) {
                    //  $("#windown-title h2").html(title);
                    $("#windown-content").html("<iframe id=\"windown-iframe\" src=\"" + content + "\" width=\"100%\" height=\"" + parseInt(height) + "px" + "\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
                }
            });
            break;
    }  //end Div Init 
    //标题
    $("#windown-title h2").html(title);


    if (height >= 527) {
        $("#windown-title").css({ width: (parseInt(width) + 22) + "px" });
        $("#windown-content").css({ width: (parseInt(width) + 17) + "px", height: height + "px" });
    } else {
        $("#windown-title").css({ width: (parseInt(width) + 10) + "px" });
        $("#windown-content").css({ width: width + "px", height: height + "px" });
    }
    var cw = document.documentElement.clientWidth, ch = document.documentElement.clientHeight, est = document.documentElement.scrollTop;
    var _version = $.browser.version;
    if (_version == 6.0) {
        $("#windown-box").css({ left: "50%", top: (parseInt((ch) / 2) + est) + "px", marginTop: -((parseInt(height) + 53) / 2) + "px", marginLeft: -((parseInt(width) + 32) / 2) + "px", zIndex: "999999" });
    } else {
        $("#windown-box").css({ left: "50%", top: "50%", marginTop: -((parseInt(height) + 53) / 2) + "px", marginLeft: -((parseInt(width) + 32) / 2) + "px", zIndex: "999999" });
    };

    //判断是否显示背景
    if (options.showBg) {
        $("#windownbg").show();
        $("#windownbg").animate({ opacity: "0.5" }, "normal"); //设置透明度
    } else {
        $("#windownbg").remove();
    };
    //可移动..必须引用 -- jquery.ui.-8.customer.js

    if (options.dragable) {
        $("#windown-box").draggable({ handle: '#windown-title,#windown-box-controller' }); //'windown-title'
        $("#windown-title").css("cursor", "move");
    }

    //自定义 内容区样式 Css Name
    if (options.contentCssClass) {
        $("#windown-content").attr("class", contentCssClass);
    }

    /*时间到后触发此函数*/
    var closeWindown = function () {
        $("#windownbg").remove();
        $("#windown-box").fadeOut("slow", function () {
            $(this).remove();
            if (options.onClose != null && (typeof options.onClose != 'undefined')) {
                options.onClose();
            }
        });
        if (options.autoUrl) {//判断是否跳转
            //window.location.href=url;
            window.frames["main"].location.href = options.autoUrl;
        }
    }
    //显示 弹出层
    $("#windown-box").show();

    if (typeof (options.showCloseBtn) == 'undefined')
    { options.showCloseBtn = true; }

    //注册事件.. 
    if (!options.showCloseBtn || typeof (options.timeOut) == 'undefined') {
        //隐藏关闭按钮
        $("#windown-close").hide();
    }
    //注册确定 和 取消 按钮事件  
    if (options.isConfirm && typeof (options.isConfirm) != 'undefined') {
        //清除自动关闭
        clearInterval(autoCloseTimer);
    }
    else //自动关闭  ( confirm 和 自动关闭时互斥的..
    {
        if (options.timeOut && typeof (options.timeOut) != 'undefined') {
            clearInterval(autoCloseTimer);
            autoCloseTimer = setTimeout(closeWindown, options.timeOut);
        }
    } //end of IsConfirm   
    //确定按钮.
    $("#windown-btn-ok").click(function () {

        if (typeof options.onOk != 'undefined') {
            //TODO:这个设置待考虑
            var e = callBackArg; // { handled: true };
            e.handled = true;
            e.Sender = $(this);
            e.Text = $(this).val();
            options.onOk(e); //设置一个回调返回值..
            //.返回为true 才关闭..
            if (callBackArg.handled) {// if (callBackArg || (callBackArg == null || callBackArg == 'undefined')) {
                directDoClose();
            }
            if (typeof options.onClosed != 'undefined') {
                options.onClosed();
            }
        } else directDoClose();
    });

    //取消按钮
    $("#windown-btn-cancel").click(function () {
        //UNDONE:这里 有个场景..比如按 取消..后 弹出一个提示是否取消..否 就不关闭弹出层...[暂时没有实现这个..]
        if (typeof options.onCancel != 'undefined') {
            callBackArg.Text = $(this).val();
            callBackArg.handled = true;
            callBackArg.Sender = this;
            options.onCancel(callBackArg);
            if (callBackArg.handled)
            { directDoClose(); }
        } else directDoClose();
    });

    if (!options.isConfirm || typeof options.isConfirm == 'undefined') {
        //关闭按钮和右上角的关闭按钮事件相同..为 onCancel 按钮的click 事件  
        //只有确定按钮 
        //那么 右上角的关闭和 确定按钮共享事件..
        $("#windown-close").click(function () {
            if (!options.showBottomPanel || typeof options.showBottomPabel == 'undefined') {
                callBackArg.Text = "关闭";
                callBackArg.handled = true;
                callBackArg.Sender = this;
                if (typeof (options.onClose) != 'undefined') {
                    options.onClose(callBackArg);
                }

                if (callBackArg.handled)
                { directDoClose(); }
            }
            else
                $("#windown-btn-ok").click();
        });

    } //关闭图标
    else {
        $("#windown-close").click(function () {
            if (typeof options.onClose != 'undefined') {
                callBackArg.Text = "关闭";
                callBackArg.handled = true;
                callBackArg.Sender = this;
                options.onClose(callBackArg);
                if (callBackArg.handled)
                { directDoClose(); }
            } else
                directDoClose();
        });
    }
    //注册关闭按钮事件  
    //doClose(options.onClose);
    $("#windown-btn-ok").focus();
};



//关闭弹出层..指定一个 关闭之后的回调..
function doClose(funcAcfclose) {
    $("#windown-close").click(function () {
        //TODO:测试 
        $("#windownbg").remove();
        //清除timer 
        clearInterval(autoCloseTimer);
        $("#windown-box").fadeOut("slow", function () {
            $(this).remove();
            if ((typeof funcAcfclose != 'undefined') && funcAcfclose != null) {
                funcAcfclose(BoxTag);
            }
        });
    });
}

//直接关闭弹出层..不通过 关闭按钮..
function directDoClose() {
    //TODO:测试 
    $("#windownbg").remove();
    $("#windown-box").fadeOut("slow", function () {
        $(this).remove();
    });
}


//通过关闭按钮关闭弹出层..1
function closeBox() {
    $("#windown-close").click();
}

//通过关闭按钮关闭弹出层..2
function tipsClose() {
    closeBox();
}

//创建默认设置的参数..
//默认: title .text: content ,高:200,宽300 , 可移动 ,
//显示关闭按钮,不自动关闭,显示遮罩,不自动跳转, 
//onClose=null,onOk = null,onCancel = null,isConfirm=false; 
function getDefaultOptions(opt) {

    if (typeof (opt.showBg) == 'undefined') {
        opt.showBg = true;
    }
    if (typeof opt.showCloseBtn == 'undefined')
    { opt.showCloseBtn = true; }
    if (typeof (opt.width) == 'undefined')
    { opt.width = 400; }
    if (typeof (opt.height) == 'undefined')
    { opt.height = 300; }
    if (typeof (opt.isConfirm) == 'undefined')
    { opt.isConfirm = false; }
    if (typeof (opt.showBottomPanel) == 'undefined')
    { opt.showBottomPanel = false; }
    if (typeof (opt.autoUrl) == 'undefined')
    { opt.autoUrl = ""; }
    if (typeof (opt.timeOut) == 'undefined')
    { opt.timeOut = ""; }
    if (typeof (opt.contextCssClass) == 'undefined')
    { opt.contentCssClass = ""; }
    if (typeof (opt.dragable) == 'undefined')
    { opt.dragable = false; }
    if (!opt.onClose || typeof (opt.onClose) == 'undefined') {
        //  opt.onClose = undefined;
    } // function () { }; }
    if (!opt.onOk || typeof (opt.onOk) == 'undefined') {
        //   opt.onOk = undefined; 
    }
    if (!opt.onCancel || typeof (opt.onCancel) == 'undefined') {
        // opt.onCancel = undefined; 
    }
    if (opt.showBottomPanel) {
        if (typeof (opt.onOk) == 'undefined') {
            opt.onOk = directDoClose;
        }
    }
    return opt;
}


//方法重载..
//适配器模式.. 

//重载主调方法
//1.
tipsWindown = function (title, content, width, height, drag, time, url, showbg, cssName, _showCloseBtn) {

    tipsWindow(getOptionsFromArgument(title, content, width, height, drag, time, url, showbg, cssName, _showCloseBtn, undefined, false));
};


//2.一个显示指定的标题,内容,高度..是否可拖动,自动关闭时间,自动转到的url,是否遮罩,默认显示关闭按钮 的重载
tipsWindown = function (title, content, width, height, drag, time, url, showbg) {
    tipsWindow(getOptionsFromArgument(title, content, width, height, drag, time, url, showbg, "", true, undefined, false));
};

tipsConfirm = function (title, context, width, height, onOk, onCancel) {
    tipsWindow({
        title: title,
        content: context,
        dragable: true,
        showCloseBtn: true,
        timeOut: "",
        width: width,
        height: height,
        isConfirm: true,
        onOk: onOk,
        onCancel: onCancel
    });
};

//4.一个显示指定的标题,内容  ,默认宽度300,高度 150 的 默认可以拖动 的,3秒自动关闭 不自动跳转页面的 遮罩父窗口的显示关闭按钮的弹出层的重载
tipsAlert = function (content, title) {
    tipsWindow(getOptionsFromArgument(title, "text:" + content, 300, 100, true, "3500", "", true, "", true, undefined, false));
};
tipsAlertWithSize = function (content, title, width, height) {
    tipsWindow(getOptionsFromArgument(title, "text:" + content, width, height, true, "", "", false, "", true, undefined, false));
};
//5 . 一个显示指定的标题,内容  ,默认宽度300,高度 150 的 默认可以拖动 的,2秒自动关闭 不自动跳转页面的 不遮罩父窗口的显示关闭按钮的弹出层的重载
tipsHelpAlert = function (content, title, width, height) {
    tipsWindow(getOptionsFromArgument(title, "text:" + content, width == undefined ? 300 : width, height == undefined ? 100 : height, true, "", "", false, "", true, undefined, false));
};

// 6.同5 ..=>自动关闭
tipsHelpAlertAndClose = function (content, title, width, height, timeOut) {
    tipsWindow(getOptionsFromArgument(title, "text:" + content, width == undefined ? 300 : width, height == undefined ? 100 : height, true,
    timeOut == undefined ? "3000" : timeOut, "", false, "", true, null, false));
};

tipsToPageWithPanel = function (title, iFrameUrl, width, height) {
    tipsWindow({
        title: title,
        type: "iframe",
        content: iFrameUrl,
        width: width, 
        showBottomPanel: true,
        height: height,
        dragable: true,
        showCloseBtn: true,
        onClose: undefined
    });
}

//7.TipsToNewPage ..在弹出层打开 Iframe
tipsToPage = function (title, iFrameUrl, width, height, onBoxClosedCallBack) {
    tipsWindow({
        title: title,
        type: "iframe",
        content: iFrameUrl,
        width: width,
        height: height,
        dragable: true,
        showCloseBtn: true,
        onClose: onBoxClosedCallBack
    });
}
//7-1 同 7 .TipsToNewPage ..在弹出层打开 Iframe,默认不指定关闭层之后的方法.
tipsToPageNoneCallBack = function (title, iFrameUrl, width, height) {
    tipsWindow({
        title: title,
        type: "iframe",
        content: iFrameUrl,
        width: width,
        height: height,
        dragable: true,
        showCloseBtn: true
    });
}

//弹出并自动跳转
tipsAlertAndGoTo = function (content, title, autoGoPageUrl) {
    var arg = { title: title, content: content, autoUrl: autoGoPageUrl, timeOut: 3000, dragable: true, showBg: true, showCloseBtn: false, isConfirm: false };

    tipsWindow(arg);
}


//适配方法
function getOptionsFromArgument(title, content, width, height, drag, time, url, _showbg, cssName, _showCloseBtn, onBoxClosedCallBack, _isConfirm) {

    var _type = content.substring(0, content.indexOf(":"));
    var _content = content.substring(content.indexOf(":") + 1, content.length);

    return {
        title: title,
        type: _type,
        content: _content,
        width: width,
        height: height,
        dragable: drag,
        timeOut: time,
        autoUrl: url,
        showBg: _showbg,
        contentCssClass: cssName,
        showCloseBtn: _showCloseBtn,
        isConfrim: _isConfirm,
        onClose: typeof onBoxClosedCallBack == 'undefined' || onBoxClosedCallBack == null ? undefined : onBoxClosedCallBack,
        showBottomPanel: true
    }
}

function tipsSetting(op) {
    if (typeof op.BottomPanelVisable != 'undefined') {
        if (op.BottomPanelVisable)
            $("#windown-box-controller").show();
        else
            $("#windown-box-controller").hide();
    }

    if (typeof op.WindownIframeScroll != 'undefined') {
        if (!op.WindownIframeScroll)
            $("#windown-iframe").attr("scrolling", "no");
    }
}